package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonPage;
import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.HospitalOutpatientParam;
import com.btpj.hospital_server.entity.HospitalClinic;
import com.btpj.hospital_server.entity.HospitalOutpatient;
import com.btpj.hospital_server.service.HospitalClinicService;
import com.btpj.hospital_server.service.HospitalInfoService;
import com.btpj.hospital_server.service.HospitalOutpatientService;
import com.btpj.hospital_server.service.HospitalSpecialService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 医院门诊Controller
 *
 * @author BTPJ  2020/8/27
 */
@Api(tags = "医院门诊接口")
@RestController
@RequestMapping("/outpatient")
public class HospitalOutpatientController {

    @Resource
    private HospitalOutpatientService hospitalOutpatientService;

    @Resource
    private HospitalSpecialService hospitalSpecialService;

    @Resource
    private HospitalClinicService hospitalClinicService;

    @Resource
    private HospitalInfoService hospitalInfoService;

    @ApiOperation("添加门诊信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "门诊名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "specialId", value = "所属专科编号", required = true, dataType = "Long", paramType = "query"),
    })
    @PostMapping("/add")
    public CommonResult<Void> addOutpatient(HospitalOutpatientParam param) {
        String name = param.getName();
        if (name == null || name.trim().isEmpty()) {
            return CommonResult.error("门诊名称不可为空");
        }
        if (hospitalOutpatientService.existOutpatient(name)) {
            return CommonResult.error("已存在此名称的门诊");
        }
        if (!hospitalSpecialService.existSpecial(param.getSpecialId())) {
            return CommonResult.error("门诊所属的专科编号不存在");
        }
        if (hospitalOutpatientService.addOutpatient(param)) {
            return CommonResult.success("添加门诊信息成功");
        }

        return CommonResult.error("服务器错误，添加门诊信息失败");
    }

    @ApiOperation("修改门诊信息")
    @ApiImplicitParam(name = "id", value = "门诊编号", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/update/{id}")
    public CommonResult<Void> updateOutpatient(@PathVariable Long id, HospitalOutpatientParam param) {
        if (!hospitalOutpatientService.existOutpatient(id)) {
            return CommonResult.error("不存在指定编号的门诊");
        }

        String name = param.getName();
        if (name != null) {
            if (name.trim().isEmpty()) {
                return CommonResult.error("门诊名不可为空");
            }
            if (hospitalSpecialService.existSpecial(name)) {
                return CommonResult.error("已存在此名称的门诊");
            }
        }

        if (param.getSpecialId() != null && !hospitalSpecialService.existSpecial(param.getSpecialId())) {
            return CommonResult.error("门诊所属的专科编号不存在");
        }

        if (hospitalOutpatientService.updateOutpatient(id, param)) {
            return CommonResult.success("修改门诊信息成功");
        }

        return CommonResult.error("服务器错误，修改门诊信息失败");
    }

    @ApiOperation("删除门诊信息")
    @ApiImplicitParam(name = "id", value = "门诊编号", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> updateOutpatient(@PathVariable Long id) {
        if (!hospitalOutpatientService.existOutpatient(id)) {
            return CommonResult.error("不存在指定编号的门诊");
        }

        if (hospitalOutpatientService.deleteOutpatient(id)) {
            return CommonResult.success("删除门诊信息成功");
        }

        return CommonResult.error("服务器错误，删除门诊信息失败");
    }

    @ApiOperation("获取门诊信息")
    @ApiImplicitParam(name = "id", value = "门诊编号", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/detail/{id}")
    public CommonResult<HospitalOutpatient> getOutpatientDetails(@PathVariable Long id) {
        if (!hospitalOutpatientService.existOutpatient(id)) {
            return CommonResult.error("不存在指定编号的门诊");
        }

        HospitalOutpatient outpatient = hospitalOutpatientService.getOutpatientDetails(id);
        if (outpatient != null) {
            return CommonResult.success(outpatient);
        }

        return CommonResult.error("服务器错误，获取门诊信息失败");
    }

    @ApiOperation(value = "获取门诊分页列表", notes = "可通过名称、所属医院、所属专科筛选查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "门诊名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hospitalId", value = "医院编号", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "specialId", value = "专科编号", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, paramType = "query", dataType = "Int"),
    })
    @GetMapping("/pageList")
    public CommonResult<CommonPage<HospitalOutpatient>> getOutpatientPageList(@RequestParam(required = false) String name,
                                                                              @RequestParam(required = false) Long hospitalId,
                                                                              @RequestParam(required = false) Long specialId,
                                                                              @RequestParam Integer pageNo,
                                                                              @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalOutpatient> commonPage = CommonPage.listPage(hospitalOutpatientService.getList(name, hospitalId, specialId));
        return CommonResult.success(commonPage);
    }

    @ApiOperation(value = "获取门诊包含的诊室分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "门诊编号", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, paramType = "query", dataType = "Int"),
    })
    @GetMapping("/clinicPageList/{id}")
    public CommonResult<CommonPage<HospitalClinic>> getClinicPageList(@PathVariable Long id,
                                                                      @RequestParam Integer pageNo,
                                                                      @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalClinic> commonPage = CommonPage.listPage(hospitalClinicService.getClinicListByOutpatientId(id));
        return CommonResult.success(commonPage);
    }
}
